package xyz.syyrjx.provider.load;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import xyz.syyrjx.provider.SyyrjxRpcProviderClass;
import xyz.syyrjx.provider.conf.SyyrjxRpcProviderConfiguration;
import xyz.syyrjx.provider.exception.SyyrjxRpcProviderException;

import java.util.Arrays;

/**
 * @author 大地崩坏苍蝇兽
 * @date 2023/1/9 - 10:19
 */
public class SyyrjxRpcProviderServerLoadAware implements ApplicationContextAware {

    private final static Logger LOGGER = LoggerFactory.getLogger(SyyrjxRpcProviderServerLoadAware.class);

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        LOGGER.debug("提供者服务端启动自动调用");
        SyyrjxRpcProviderClass providerLoader = applicationContext.getBean(SyyrjxRpcProviderClass.class);
        LOGGER.debug("注册中心服务器数量" + SyyrjxRpcProviderConfiguration.getConfiguration().getRegistryAddress().length);
        try {
            providerLoader.createProviderMap(applicationContext);
            providerLoader.providerServerRun();
        }catch (Exception e) {
            throw new SyyrjxRpcProviderException(SyyrjxRpcProviderException.PROVIDER_SERVER_RUN_ERROR, e);
        }
    }
}
